/*		Filename: 	net_manager.h
 *		Author:		彭望
 *		Date:		2012-01-12
 *		Use:		实现网络通信管理器的结构
 */

#ifndef NET_MANAGER_H
#define NET_MANAGER_H
//#define BOOST_ASIO_ENABLE_HANDLER_TRACKING

#include "public.h"
#include "logger.h"
#include <map>
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/thread.hpp>
using namespace boost::asio;
using namespace boost::asio::ip;
using boost::function;


class CNetManager : public enable_shared_from_this<CNetManager>
{
		public:
				typedef ip::tcp::endpoint endpoint;
				typedef ip::tcp::socket sock_t;
				typedef ip::tcp::acceptor acceptor;
				typedef ip::tcp::resolver resolver;

				typedef boost::shared_ptr<sock_t> SockPtr;
				typedef string SessionIndex_t;
				typedef std::map<SessionIndex_t, SockPtr>  sock_map_t;

				typedef function< void (const SessionIndex_t& , istream& , ostream& ) > server_callback_func_t;
				typedef function< void (istream&, ostream&) > client_callback_func_t;
				typedef function< void (bool isConnected) > client_connect_callback_func_t;

				typedef boost::asio::streambuf buffer_t;
				typedef shared_ptr<buffer_t> BufferPtr; 
				typedef boost::system::error_code error_code;

				typedef std::pair<BufferPtr, BufferPtr> BufferPair;
				typedef std::map<SessionIndex_t, BufferPair> buffer_map_t;
				const static size_t MAX_MESSAGE_LENGTH = 1 << 20;

				CNetManager(io_service& ios);

				RetCode_t init();

				//用于服务器端监听消息
				RetCode_t async_server_listen(const string& bind_ip, short port, server_callback_func_t func);
				//RetCode_t async_client_listen(const string& bind_ip, short port, client_callaback_func_t func);
				void handle_server_accept(const error_code& ec, const server_callback_func_t& func, SockPtr sock);
				void handle_server_read(const error_code& ec, size_t bytes_transferred, const server_callback_func_t& func, SockPtr sock);

				void handle_server_write(const error_code& ec, size_t bytes_transferred, 
										server_callback_func_t, SockPtr);
				void handle_request(const BufferPtr& req, BufferPtr& res, const server_callback_func_t& func, const SessionIndex_t& index);




				//用于客户端
				void handle_client_read(const error_code& ec, size_t bytes_transferred, 
									const client_callback_func_t& callback);
				void handle_client_write(const error_code& ec, size_t bytes_transferred, 
									const client_callback_func_t& callback);
				void handle_client_resolve(const error_code& ec, tcp::resolver::iterator iter, client_connect_callback_func_t callback);
				void handle_client_connect(const error_code& ec, client_connect_callback_func_t callback);

				//连接某个地址
				RetCode_t connect(const string& server_ip, short server_listen_port, client_connect_callback_func_t callback);
				//向某个地址发送数据
				RetCode_t send(const string& msg, const client_callback_func_t& callback);

				//用于保持io_service时刻有注册事件，不会返回
				void handle_io_service_keeper(const error_code& ec);
				

				static SockPtr make_sock(io_service& ios);
				static BufferPtr make_buffer();
				static SessionIndex_t make_session_index(SockPtr sock);
		private:
				RetCode_t start_accept(server_callback_func_t );

		private:
				boost::posix_time::seconds m_timer_period;
				io_service& m_ios;
				Logger::Logger_t& logger;
				acceptor m_acceptor;
				SockPtr m_connSock;
				resolver m_resolver;
				/*
				BufferPtr m_sendBuffer;
				BufferPtr m_recvBuffer;
				*/
				deadline_timer m_io_service_keeper_timer;

				sock_map_t m_socks;
				buffer_map_t m_buffers;

};

typedef boost::shared_ptr<CNetManager> CNetManagerPtr;

#endif

